﻿// Decompiled with JetBrains decompiler
// Type: NetickEditor.NetworkObjectEditor
// Assembly: Netick.Unity.Editor, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 0658BB4A-05C4-428D-9BD6-82E94959F5A1
// Assembly location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Editor\Netick.Unity.Editor.dll

using Netick.Unity;
using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;

#nullable disable
namespace NetickEditor;

[CustomEditor(typeof (NetworkObject), true)]
[CanEditMultipleObjects]
public class NetworkObjectEditor : Editor
{
  private NetworkObject Object;
  private SerializedProperty PersistentProperty;
  private SerializedProperty AddToNetworkLoop;
  private SerializedProperty PredictionModeProperty;
  private SerializedProperty AoILayer;
  private SerializedProperty BroadPhaseFiltering;
  private SerializedProperty NarrowPhaseFiltering;
  private SerializedProperty UseSAP;
  private const string SandboxTooltip = "The sandbox of this network object.";
  private const string InputSourceTooltip = "The player id of the input source of this network object.";
  private const string NetworkIdTooltip = "The network id of this object. It's -1 if the object is not yet added to the network simulation. Which could happen if the object has not yet been received by the client.";
  internal const string PredictionModeTooltip = "Choose whether this object will be resimulated on every client or only the client who's the input source.";
  internal const string AddToNetworkLoopTooltip = "Disable this to make Netick not invoke network loop callbacks such as NetworkFixedUpdate and NetworkRender, on this object.\r\n\r\nThis can be useful for advanced performance optimizations. ";
  internal const string PersistentTooltip = "Enable this to mark this prefab as DontDestroyOnLoad, preventing its instances from being destroyed when changing scenes. Only valid on network prefabs.";
  private const string BroadPhaseFilteringTooltipTooltip = "Choose what set of players are interested in receiving updates to this object. This will only have an effect when Interest Management is enabled. To enable it, go to Netick -> Settings -> Interest Management.";
  private const string NarrowPhaseFilteringTooltip = "Enable this if you want to be able to filter-send this object to specific clients, by explicit control. Don't conflict this with Broad Phase Filter. This will only have an effect when Interest Management and Narrow Phase Filterting are enabled. To enable them, go to Netick -> Settings -> Interest Management.";
  internal const string RelevanceTooltip = "Choose whether this object will be replicated to everyone or only the client who's the input source.";

  private void OnEnable()
  {
    this.Object = (NetworkObject) this.target;
    this.PersistentProperty = this.serializedObject.FindProperty("_IsPersistent");
    this.PredictionModeProperty = this.serializedObject.FindProperty("_PredictionMode");
    this.AddToNetworkLoop = this.serializedObject.FindProperty("_AddToNetworkLoop");
    this.AoILayer = this.serializedObject.FindProperty("_AoILayer");
    this.BroadPhaseFiltering = this.serializedObject.FindProperty("_BroadPhaseFilter");
    this.NarrowPhaseFiltering = this.serializedObject.FindProperty("_NarrowPhaseFilter");
    this.UseSAP = this.serializedObject.FindProperty("_SpatialPrioritization");
  }

  public static void HorizontalLine(Color color, float height, Vector2 margin)
  {
    GUILayout.Space(margin.x);
    EditorGUI.DrawRect(EditorGUILayout.GetControlRect(false, height, Array.Empty<GUILayoutOption>()), color);
    GUILayout.Space(margin.y);
  }

  public virtual void OnInspectorGUI()
  {
    this.serializedObject.Update();
    NetworkObject targetObject = (NetworkObject) this.serializedObject.targetObject;
    if (Application.isPlaying && (UnityEngine.Object) targetObject != (UnityEngine.Object) null && (UnityEngine.Object) targetObject.Sandbox != (UnityEngine.Object) null && targetObject.Id < 0)
    {
      Color color = GUI.color;
      GUI.color = Color.yellow;
      NetickEditorProperty.DrawLabel(this.serializedObject.targetObject, "Unlinked", "", "This NetworkObject is not linked to simulation.");
      GUI.color = color;
    }
    if (NetickEditorUtils.IsUninstantiatedPrefab(this.Object.gameObject) || this.Object.gameObject.IsBeingEditedInIsolatedPrefabMode())
    {
      EditorGUILayout.HelpBox("When you add or remove child network objects of this prefab, make sure to click Register Scene Objects with every scene where instances of this prefab are scene objects.", (MessageType) 1);
      if (GUILayout.Button("Register Scene Objects"))
        NetickEditorUtils.ConfigureNetworkSceneObjects();
      EditorGUILayout.LabelField("\n", Array.Empty<GUILayoutOption>());
    }
    else if (this.Object._SceneId == -1 && (!Application.isPlaying || PrefabUtility.IsPartOfAnyPrefab((UnityEngine.Object) this.Object.gameObject)))
      EditorGUILayout.HelpBox("This object is not registered properly. To fix this, click Register Scene Objects in Netick -> Settings", (MessageType) 2);
    if ((UnityEngine.Object) this.Object.Sandbox != (UnityEngine.Object) null)
    {
      Scene scene = this.Object.gameObject.scene;
      if (Application.isPlaying)
        NetickEditorProperty.DrawLabel(this.serializedObject.targetObject, "Sandbox ", this.Object.Sandbox.Name, "The sandbox of this network object.");
    }
    Scene scene1 = this.Object.gameObject.scene;
    if ((1 & (this.Object.Entity != null ? 1 : 0) & (Application.isPlaying ? 1 : 0)) != 0)
      NetickEditorProperty.DrawLabel(this.serializedObject.targetObject, "Network Id ", this.Object.Id.ToString(), "The network id of this object. It's -1 if the object is not yet added to the network simulation. Which could happen if the object has not yet been received by the client.");
    else
      NetickEditorProperty.DrawLabel(this.serializedObject.targetObject, "Network Id ", "Determined at Runtime", "The network id of this object. It's -1 if the object is not yet added to the network simulation. Which could happen if the object has not yet been received by the client.");
    if ((UnityEngine.Object) this.Object.Sandbox != (UnityEngine.Object) null)
    {
      Scene scene2 = this.Object.gameObject.scene;
      if (Application.isPlaying)
      {
        string text = $"{this.Object.InputSourcePlayerId}";
        if (this.Object.Sandbox.LocalPlayer.PlayerId == this.Object.InputSourcePlayerId)
          text += " (Local Player)";
        if (!this.Object.InputSourcePlayerId.IsValid)
          text = "null";
        NetickEditorProperty.DrawLabel(this.serializedObject.targetObject, "Input Source Player Id ", text, "The player id of the input source of this network object.");
      }
    }
    EditorGUI.BeginDisabledGroup(Application.isPlaying);
    NetickEditorProperty.DrawProperty(this.serializedObject.targetObject, this.PredictionModeProperty, "Choose whether this object will be resimulated on every client or only the client who's the input source.");
    NetickEditorProperty.DrawProperty(this.serializedObject.targetObject, this.AddToNetworkLoop, "Disable this to make Netick not invoke network loop callbacks such as NetworkFixedUpdate and NetworkRender, on this object.\r\n\r\nThis can be useful for advanced performance optimizations. ");
    if (!this.Object.gameObject.scene.IsValid() || this.Object.gameObject.IsBeingEditedInIsolatedPrefabMode() || Application.isPlaying)
      NetickEditorProperty.DrawProperty(this.serializedObject.targetObject, this.PersistentProperty, "Enable this to mark this prefab as DontDestroyOnLoad, preventing its instances from being destroyed when changing scenes. Only valid on network prefabs.");
    EditorGUI.EndDisabledGroup();
    if (NetickWindow.IsP)
    {
      NetickEditorToggleState.ShowObjectIMSettings = EditorGUILayout.BeginFoldoutHeaderGroup(NetickEditorToggleState.ShowObjectIMSettings, "Interest Management", (GUIStyle) null, (Action<Rect>) null, (GUIStyle) null);
      if (NetickEditorToggleState.ShowObjectIMSettings)
      {
        EditorGUI.BeginDisabledGroup(Application.isPlaying);
        NetickEditorProperty.DrawProperty(this.serializedObject.targetObject, this.BroadPhaseFiltering, "Choose what set of players are interested in receiving updates to this object. This will only have an effect when Interest Management is enabled. To enable it, go to Netick -> Settings -> Interest Management.");
        if (this.BroadPhaseFiltering.enumValueIndex == 1)
          NetickEditorProperty.DrawProperty(this.serializedObject.targetObject, this.AoILayer);
        NetickEditorProperty.DrawProperty(this.serializedObject.targetObject, this.NarrowPhaseFiltering, "Enable this if you want to be able to filter-send this object to specific clients, by explicit control. Don't conflict this with Broad Phase Filter. This will only have an effect when Interest Management and Narrow Phase Filterting are enabled. To enable them, go to Netick -> Settings -> Interest Management.");
        EditorGUI.EndDisabledGroup();
      }
      EditorGUILayout.EndFoldoutHeaderGroup();
    }
    NetickEditorToggleState.ShowAdvancedNetworkObject = EditorGUILayout.BeginFoldoutHeaderGroup(NetickEditorToggleState.ShowAdvancedNetworkObject, "Info", (GUIStyle) null, (Action<Rect>) null, (GUIStyle) null);
    if (NetickEditorToggleState.ShowAdvancedNetworkObject)
    {
      if (Application.isPlaying)
        EditorGUILayout.LabelField("Prefab Id ", this.Object.PrefabId.ToString(), EditorStyles.label, Array.Empty<GUILayoutOption>());
      EditorGUILayout.LabelField("Scene Id", this.Object.SceneId.ToString(), Array.Empty<GUILayoutOption>());
      if (Application.isPlaying)
        EditorGUILayout.LabelField("Runtime Scene Id", this.Object.RuntimeSceneId.ToString(), Array.Empty<GUILayoutOption>());
      EditorGUILayout.LabelField("Is Scene Object", this.Object.IsSceneObject.ToString(), Array.Empty<GUILayoutOption>());
    }
    EditorGUILayout.EndFoldoutHeaderGroup();
    this.serializedObject.ApplyModifiedProperties();
  }
}
